// 图像化编程的代码演示

#if 0
movecar(v_m1a, v_m1b, v_m2a, v_m2b) {
	v_m1a=((v_m1a) \ 1.28);
	v_m1b=((v_m1b) \ 1.28);
	v_m2a=((v_m2a) \ 1.28);
	v_m2b=((v_m2b) \ 1.28);
	if(((v_m1a) > 0)) {
		motorRun("M1A",(v_m1a),"forward");
	} else {
		if(((v_m1a) < 0)) {
			motorRun("M1A",(-(v_m1a)),"reverse");
		} else {
			motorRun("M1A",0,"stop");
		}
	}
	if(((v_m1b) > 0)) {
		motorRun("M1B",(v_m1b),"forward");
	} else {
		if(((v_m1b) < 0)) {
			motorRun("M1B",(-(v_m1b)),"reverse");
		} else {
			motorRun("M1B",0,"stop");
		}
	}
	if(((v_m2a) > 0)) {
		motorRun("M2A",(v_m2a),"forward");
	} else {
		if(((v_m2a) < 0)) {
			motorRun("M2A",(-(v_m2a)),"reverse");
		} else {
			motorRun("M2A",0,"stop");
		}
	}
	if(((v_m2b) > 0)) {
		motorRun("M2B",(v_m2b),"forward");
	} else {
		if(((v_m2b) < 0)) {
			motorRun("M2B",(-(v_m2b)),"reverse");
		} else {
			motorRun("M2B",0,"stop");
		}
	}
}
var fb_speed;
var rl_speed;
var turn_speed;
var cmdSum;
var reduction;
while (true) {
	fb_speed=(webJoystickRevieve(0,1));
	rl_speed=(webJoystickRevieve(0,0));
	turn_speed=(webJoystickRevieve(1,0));
	cmdSum=(abs(fb_speed) + abs(rl_speed) + abs(turn_speed));
	if ((cmdSum) > 128) {
		reduction=(128 \ (cmdSum));
		fb_speed=((fb_speed) * (reduction));
		rl_speed=((rl_speed) * (reduction));
		turn_speed=((turn_speed) * (reduction));
	}
	MoveCar((fb_speed) + (rl_speed) + (turn_speed), ((fb_speed) - (rl_speed)) - (turn_speed), ((fb_speed) + (rl_speed)) - (turn_speed), ((fb_speed) - (rl_speed)) + (turn_speed));
	delay(10);
}
#endif

/**
 * @brief  获取摇杆数值
 * @param  joyID       摇杆ID
 * @param  value       x或y
 * @return int 返回摇杆数值
 */
int webJoystickRevieve(int joyID, int value) {
    // 摇杆ID - 0：左侧摇杆，1：右侧摇杆
    // x或y   - 0：获取摇杆x值，1：获取摇杆y值

    // 示例中值的范围是 -128 ~ 127
}


/**
 * @brief  控制电机运动
 * @param  motor       要控制的电机名
 * @param  V           电机的速度
 * @param  F           电机运动的方向（正转还是反转）
 */
void motorRun(char *motor, int V, char *F) {}


/**
 * @brief  控制车子
 * @param  v_m1a       M1A电机的速度
 * @param  v_m1b       M1B电机的速度
 * @param  v_m2a       M2A电机的速度
 * @param  v_m2b       M2A电机的速度
 */
void MoveCar(int v_m1a, int v_m1b, int v_m2a, int v_m2b) {
    v_m1a = (v_m1a / 1.28);
    v_m1b = (v_m1b / 1.28);
    v_m2a = (v_m2a / 1.28);
    v_m2b = (v_m2b / 1.28);

    if (v_m1a > 0) {
        motorRun("M1A", v_m1a ,"forward");
    }
    else {
        if (v_m1a < 0) {
            motorRun("M1A", -v_m1a, "reverse");
        }
        else {
            motorRun("M1A", 0, "stop");
        }
    }

    if (v_m1b > 0) {
        motorRun("M1B", v_m1b, "forward");
    }
    else {
        if (v_m1b < 0) {
            motorRun("M1B", -v_m1b, "reverse");
        }
        else {
            motorRun("M1B", 0, "stop");
        }
    }

    if (v_m2a > 0) {
        motorRun("M2A", v_m2a, "forward");
    }
    else {
        if (v_m2a < 0) {
            motorRun("M2A", -v_m2a, "reverse");
        }
        else {
            motorRun("M2A", 0, "stop");
        }
    }

    if (v_m2b > 0) {
        motorRun("M2B", v_m2b, "forward");
    }
    else {
        if (v_m2b < 0) {
            motorRun("M2B", -v_m2b, "reverse");
        }
        else {
            motorRun("M2B", 0, "stop");
        }
    }
}

int _main_ ()
{
    int Y;    // 前后移动的速度
    int X;    // 左右移动的速度
    int turn_speed;  // 左右转动的速度
    int cmdSum;      // r系数
    int reduction;   // r系数

    while (1) {
        Y = webJoystickRevieve(0, 1);  // 获取左侧摇杆的Y值
        X = webJoystickRevieve(0, 0);  // 获取左侧摇杆的X值
        turn_speed = webJoystickRevieve(1, 0);  // 获取右侧摇杆的X值
        cmdSum = (abs(Y) + abs(X) + abs(turn_speed));  // 取绝对值求和

        if (cmdSum > 128) {
            reduction  = 128 / cmdSum;
            Y          = Y * reduction;
            X          = X * reduction;
            turn_speed = turn_speed * reduction;
        }

        // V合 = r * ( V前后移 + V左右移 + V转动 )
        MoveCar( Y + X  + turn_speed,
                (Y - X) - turn_speed,
                (Y + X) - turn_speed,
                (Y - X) + turn_speed);

        delay(10);
    }
}

